/***************************************************************************
 *   Copyright (C) 2009 by Dead_Body                                       *
 *   jamesholodnak@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <windows.h>

#include "defines.h"
#include "resource.h"
#include "gui2/gui2.h"

HWND hWnd;
HINSTANCE hInstance;

//from system/win32/video.cpp
extern int screenw,screenscale;

//is this bad?
extern int quit;

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	/*
	mousemove_u mm;
	mousebutton_u mb;
	int x,y,xoff,yoff,d;
	static u32 downtime,lastdowntime = 0;

	if(config.windowed > 0) {
		xoff = 0;
		yoff = 0;
	}
	else {
		xoff = (screenw - (256 * screenscale)) / 2;
		yoff = 0;
		if(screenw == 800)
			yoff = 11 * 3;
		else if(screenw == 1024)
			yoff = 11 * 3;
	}
	while(SDL_PollEvent(&event)) {
		switch(event.type) {
			case SDL_QUIT:
				quit++;
				break;
			case SDL_MOUSEMOTION:
				if((event.motion.x - xoff) < 0) {
					x = 0;
					mm.info.xrel = 0;
				}
				else {
					x = (event.motion.x - xoff) / screenscale;
					mm.info.xrel = (s8)event.motion.xrel;
				}
				if((event.motion.y - yoff) < 0) {
					y = 0;
					mm.info.yrel = 0;
				}
				else {
					y = (event.motion.y - yoff) / screenscale;
					mm.info.yrel = (s8)event.motion.yrel;
				}
				if(x >= 256)
					x = 255;
				if(y >= 256)
					y = 255;
				mm.info.x = (u8)x;
				mm.info.y = (u8)y;
				//update zapper input
				joyx = mm.info.x;
				joyy = mm.info.y;
				gui2_event(E_MOUSEMOVE,mm.data);
				break;
			case SDL_JOYAXISMOTION: 
				if (event.jaxis.axis == 0)
				{
					joystate[0] = 0;
					joystate[1] = 0;
					if (event.jaxis.value < -3200)
						joystate[0] = 1;
					else if (event.jaxis.value > 3200)
						joystate[1] = 1;
				}
				else if (event.jaxis.axis == 1)
				{
					joystate[2] = 0;
					joystate[3] = 0;
					if (event.jaxis.value < -3200)
						joystate[2] = 1;
					else if (event.jaxis.value > 3200)
						joystate[3] = 1;
				}
				break;
			case SDL_JOYBUTTONDOWN: 
				joystate[event.jbutton.button + 4] = 1;
				break;
			case SDL_JOYBUTTONUP: 
				joystate[event.jbutton.button + 4] = 0;
				break;
			case SDL_MOUSEBUTTONDOWN:
				downtime = SDL_GetTicks();
				//double click?
				if((downtime - lastdowntime) < 250)
					d = 1;
				else {
					d = 0;
					lastdowntime = downtime;
				}
			case SDL_MOUSEBUTTONUP:
				mb.info.x = (event.button.x - xoff) / screenscale;
				mb.info.y = (event.button.y - yoff) / screenscale;
				switch(event.button.button) {
					default: mb.info.btn = 0; break;
					case SDL_BUTTON_LEFT: mb.info.btn = 1; break;
					case SDL_BUTTON_RIGHT: mb.info.btn = 2; break;
					case SDL_BUTTON_WHEELUP: mb.info.btn = 3; break;
					case SDL_BUTTON_WHEELDOWN: mb.info.btn = 4; break;
				}
				gui2_event((event.button.type == SDL_MOUSEBUTTONDOWN) ? (d ? E_MOUSEDOWN2 : E_MOUSEDOWN) : E_MOUSEUP,mb.data);
				break;
		}
	}
*/
	mousemove_u mm;
	mousebutton_u mb;
	int x,y,xoff,yoff,d;
	static u32 downtime,lastdowntime = 0;

	if(config.windowed > 0) {
		xoff = 0;
		yoff = 0;
	}
	else {
		xoff = (screenw - (512 * screenscale)) / 2;
		yoff = 0;
		if(screenw == 800)
			yoff = 11 * 3;
		else if(screenw == 1024)
			yoff = 11 * 3;
	}

	switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
			break;
        case WM_DESTROY:
			quit++;
            PostQuitMessage(0);
			break;
		case WM_SIZE:
			
			break;
		case WM_LBUTTONDOWN:
			downtime = GetTickCount();
			//double click?
			d = 0;
			if((downtime - lastdowntime) < 250)
				d = 1;
			else
				lastdowntime = downtime;
		case WM_LBUTTONUP:
			mb.info.x = LOWORD(lParam) / screenscale;
			mb.info.y = HIWORD(lParam) / screenscale;
			mb.info.btn = 1;
			gui2_event((msg == WM_LBUTTONDOWN) ? (d ? E_MOUSEDOWN2 : E_MOUSEDOWN) : E_MOUSEUP,mb.data);
			break;
		case WM_MOUSEMOVE:
			if((LOWORD(lParam) - xoff) < 0) {
				x = 0;
				mm.info.xrel = 0;
			}
			else {
				x = (LOWORD(lParam) - xoff) / screenscale;
				mm.info.xrel = (s16)LOWORD(lParam);
			}
			if((HIWORD(lParam) - yoff) < 0) {
				y = 0;
				mm.info.yrel = 0;
			}
			else {
				y = (HIWORD(lParam) - yoff) / screenscale;
				mm.info.yrel = (s16)HIWORD(lParam);
			}
			if(x >= 512)
				x = 511;
			if(y >= 512)
				y = 511;
			joyx = mm.info.x = (u16)x;
			joyy = mm.info.y = (u16)y;
			gui2_event(E_MOUSEMOVE,mm.data);
			break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int window_init()
{
    WNDCLASSEX wc;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON1));
    wc.hCursor       = LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = APPCLASSNAME;
    wc.hIconSm       = LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON1));

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }

    hInstance = GetModuleHandle(0);
    // Step 2: Creating the Window

	hWnd = CreateWindowEx(
        0,
        APPCLASSNAME,
        APPNAME,
        (WS_VISIBLE | WS_SYSMENU),
        CW_USEDEFAULT, CW_USEDEFAULT, 512, 480,
        NULL, NULL, hInstance, NULL);

      SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) &~ WS_CAPTION);
      SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);

/*    hWnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        APPCLASSNAME,
        APPNAME,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 512, 480,
        NULL, NULL, hInstance, NULL);
*/
    if(hWnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }

    ShowWindow(hWnd,SW_SHOW);
    UpdateWindow(hWnd);
	ShowCursor(FALSE);
    return 0;
}

void window_kill()
{
	DestroyWindow(hWnd);
	UnregisterClass(APPCLASSNAME,hInstance);
}

void window_resize(int width,int height)
{
	RECT rw,rc;

	GetWindowRect(hWnd,&rw);
	GetClientRect(hWnd,&rc);
	SetWindowPos(hWnd,0,0,0,((rw.right - rw.left) - rc.right) + width,((rw.bottom - rw.top) - rc.bottom) + height,SWP_NOZORDER | SWP_NOMOVE);
}
